/**   
* @Title: ProductService.java 
* @Package com.csis.system.service 
* @Description: TODO
* @author dailey  
* @date 2012-10-7
* @version V1.0   
*/
package com.csis.system.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.type.Type;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.csis.framework.db.BaseDao;
import com.csis.framework.domain.Condition;
import com.csis.framework.domain.FormBean;
import com.csis.system.common.IStaticData;
import com.csis.system.common.convert.Converter.ConvertArray;
import com.csis.system.domain.Product;
import com.csis.system.domain.bean.ProductBean;

/**
 * @author dailey
 *
 */
@Service
public class ProductService {

	private Log log = LogFactory.getLog(getClass());

	@Resource
	private BaseDao baseDao;

	public Product load(Integer id) {
		return baseDao.load(Product.class, id);
	}

	public FormBean<Product> list(FormBean<Product> formBean) {
		String sql = "SELECT * FROM " + IStaticData.TABLE_PRODUCT;
		formBean.getPage().setMaxCount(getMaxCount());
		sql = formBean.getPage().perPageSql(sql);
		List<Product> products = baseDao.query4SingleTable(Product.class, sql);
		formBean.setBeans(products);
		return formBean;
	}

	public FormBean<ProductBean> unionList(FormBean<ProductBean> formBean) throws Exception {
		String sql = "SELECT product_id ,c.category_id,c.category_name,product_name,product_color,product_size,product_price1,product_price2,product_price3,product_price_unit,product_factory,product_update,product_note FROM csis.product p inner join csis.category c on(p.category_id=c.category_id) WHERE 1=1 ";
		Condition condition = formBean.getCondition();
		if (condition != null) {
			sql = condition.getCondition(sql);
		}
		formBean.getPage().setMaxCount(getMaxCount());
		sql = formBean.getPage().perPageSql(sql);
		String[] columns = new String[] { "product_id", "category_id", "category_name", "product_name",
				"product_color", "product_size", "product_price1", "product_price2", "product_price3",
				"product_price_unit", "product_factory", "product_update", "product_note" };
		Type[] types = new Type[] { Hibernate.INTEGER, Hibernate.INTEGER, Hibernate.STRING, Hibernate.STRING,
				Hibernate.STRING, Hibernate.STRING, Hibernate.FLOAT, Hibernate.FLOAT, Hibernate.FLOAT,
				Hibernate.STRING, Hibernate.STRING, Hibernate.DATE, Hibernate.STRING };
		List<ProductBean> productBeans = baseDao.query(ProductBean.class, sql, null, columns, types);
		formBean.setBeans(productBeans);
		return formBean;
	}

	public int getMaxCount() {
		String sql = "SELECT * FROM " + IStaticData.TABLE_PRODUCT;
		return baseDao.getMaxCount(sql);
	}

	/**
	 * @param prod
	 */
	public void save(Product prod) {
		log.info("ProductService>>SaveProduct>>" + prod);
		if (prod.getId() == null) {
			baseDao.persist(prod);
		} else {
			baseDao.merge(prod);
		}
	}

	@Transactional
	public void remove(Integer id) {
		baseDao.remove(Product.class, id);
	}

	@Transactional
	public void remove(ConvertArray<Integer> ids) {
		if (ids == null) {
			return;
		}
		for (Integer id : ids) {
			remove(id);
		}
	}

}
